using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Urbania.Utilities;
using Urbania.World;
using Urbania.GUI;

namespace Urbania
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        public static LuaVM lua;
        Stack<DrawableGameComponent> drawableComponents;


        public Game1(ref LuaVM l)
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            this.IsMouseVisible = true;
            lua = l; //initializes the Lua Virtual Machine
            this.drawableComponents = new Stack<DrawableGameComponent>(); //Instanciates a new stack of type DrawableComponents
            this.drawableComponents.Push(new Components.StartMenu(this, ref lua)); //adds the StartMenu to the top of the stack
            this.drawableComponents.Push(new Components.TitleAnimation(this, ref lua));

        }
        
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            Loader.InitTexLoader(GraphicsDevice, Content);
            MouseWrap.InitMouse();
            Utilities.Loader.setDir();
            Utilities.Loader.LoadScriptDir("Scripts");

            Game1.lua.Register("myQuit", this, this.GetType().GetMethod("Exit"));

            this.drawableComponents.Peek().Initialize();
            

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice); //Makes a spritebatch
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            this.drawableComponents.Peek().Update(gameTime); //Update what is on top of the stack
            base.Update(gameTime);

        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(new Color(0, 100, 200));
            this.drawableComponents.Peek().Draw(gameTime); //Draw what is on top of the stack\
            base.Draw(gameTime);
        }

        public void AddDrawableComponentToStack(DrawableGameComponent component)
        {
            drawableComponents.Push(component);
        }

        public DrawableGameComponent PopDrawableComponentOffStack()
        {
            return drawableComponents.Pop();
        }
    }
}
